﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Data;
using OgilvyOne.DALFactory;
using OgilvyOne.BLL;
using OgilvyOne.Model;
using OgilvyOne.Utility;

namespace OgilvyOne.BLL
{
    /// <summary>
    /// HTML 拼接类
    /// </summary>
    public class ContentHtmlPage
    {
        MenuModel menupage;
        public ContentHtmlPage(MenuModel menupage)
        {
            this.menupage = menupage;
        }

        /// <summary>
        /// 截取字符串长度
        /// </summary>
        /// <param name="str"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string GetSubString(string str, int length)
        {
            if (str == null) return "";
            length = length * 2;
            str = FilterHtml(str);
            string temp = str;
            int j = 0;
            int k = 0;
            for (int i = 0; i < temp.Length; i++)
            {
                try
                {
                    if (Regex.IsMatch(temp.Substring(i, 1), @"[\u4e00-\u9fa5]+"))
                    {//是否中文
                        j += 2;
                    }
                    else
                    {
                        j += 1;
                    }
                    if (j <= length)
                    {
                        k += 1;
                    }
                    if (j >= length)
                    {//终止循环
                        return temp.Substring(0, k)+"...";
                    }
                }
                catch (Exception e)
                {
                    Console.Write(e.Message);
                }
            }
            return temp;
        }
        static Regex regex = new Regex("<[^<^>]+>");
        static Regex regexHand = new Regex("&\\w+;");
        public static string FilterHtml(string html)
        {
            return regexHand.Replace(regex.Replace(html, ""),"");
        }

       
    }
}
